package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;



@Service(interfaceClass = CheckItemService.class) //TODO 使用dubbo和事务时 需要指定接口
public class  CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckItem> page = findAll(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Page<CheckItem> findAll(String queryString){
        return checkItemDao.findAll(queryString);
    }

    @Override
    public void add(CheckItem checkItem) {
        if (!checkItemDao.add(checkItem)) {
            throw new RuntimeException("添加失败，name="+checkItem.getName()+",code="+checkItem.getCode());
        }
    }

    @Override
    public void delete(Integer id) {
        if (checkItemDao.findCountByCheckItemId(id) > 0) {
            throw new RuntimeException("检查项已被引用，请取消检查组绑定");
        }

        checkItemDao.deleteById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        if(!checkItemDao.edit(checkItem)){
            throw new RuntimeException("修改失败，id="+checkItem.getId());
        }
    }

    @Override
    public CheckItem findById(Integer id) {

        return checkItemDao.findById(id);
    }
}
